//条件验证
var login = new Validated();
login.rules = {
    "name": {
        "data": /^\S{2,}$/,
        "complete": "ok",
        "error": "至少为两个汉字、英文字符或数字",
        "normal": "请用汉字、英文字符或数字",
        "required": true
    },
    "linkname": {
        "data": /^[\u4e00-\u9fa5]{2,}|[a-z]{2,}|[0-9]{2,}$/,
        "complete": "ok",
        "error": "至少为两个汉字、英文字符或数字字",
        "normal": "请用汉字、英文字符或数字",
        "required": true
    },
    "telphone": {
        "data": /^0?1[1-9]\d{9}$/,
        "complete": "ok",
        "error": "请检查电话号码是否正确",
        "normal": "请输入手机号码",
        "required": true
    },
}
login.blur().focus();
$("#purchase_create_content #table").mouseout(function () {
    if (login.complete("name", "linkname", "telphone") && $("#indate").val() != "" && $("#mainClass").children().length != 0)
    {
        var long = $('#table tbody tr').length;
        for (var i = 0; i < long; i++) {
            if ($("[name='materielname[" + i + "]']").val() == "" || $("[name='model[" + i + "]']").val() == "" || $("[name='amout[" + i + "]']").val() == "" || $("[name='unit[" + i + "]']").val() == "" || $("[name='delivery_date[" + i + "]']").val() == "")
            {
                $('.purchase_create_butn').removeClass('blue');
            } else
            {
                $('.purchase_create_butn').addClass('blue');
            }
        }
    }
})
var match;
$(".purchase_create_butn").click(function ()
{
    if (Cookie.Get("account") == "" || Cookie.Get("account") == null) {
        tanchuan("请登陆", "3", "/login/login", "1");
    } else {
        if ($("#indate").val() == "长期有效") {
            $("#indate").val("2057-10-01");
        }
        var nowtimes = new Date().getTime();
        var tim = $("#indate").val().split(" ")[0];
        tim = tim.replace(/-/g, '/');
        tim = new Date(tim).getTime();
        if (nowtimes > tim) {
            tanchuan("截止时间必须大于当前时间", "0", "", "0");
        } else {
            if (login.complete("name", "linkname", "telphone") &&  $("#indate").val() != "" && $("#mainClass").children().length != 0)
            {
                var long = $('#table tbody tr').length;
                match = long;
                for (var i = 0; i < long; i++) {
                    if ($("[name='materielname[" + i + "]']").val() == "" || $("[name='model[" + i + "]']").val() == "" || $("[name='amout[" + i + "]']").val() == "" || $("[name='delivery_date[" + i + "]']").val() == "" || $("[name='unit[" + i + "]']").val() == "" || $("[name='unit[" + i + "]']").val() == "")
                    {
                        tanchuan("请检查物料信息是否正确", "0", "", "0");
                        return;
                        $('.purchase_create_butn').removeClass('blue');
                    } else {
                        if(!isNaN( $("[name='unit[" + i + "]']").val())){
                           tanchuan("物料表中第" + (i + 1) + "行单位不能是数字", "0", "", "0");
                            return; 
                        }
                        if (isNaN($(".amout").val())) {
                            tanchuan("物料表中第" + (i + 1) + "行数量不是数字", "0", "", "0");
                            return;
                        } else {
                            match = match - 1;
                        }
                    }
                }
                if (match) {
                    tanchuan("请检查物料信息是否正确", "0", "", "0");
                }
                if (!match) {
                    var mainproducts = "";
                    $('#mainClass input').each(function () {
                        mainproducts += "," + $(this).val();
                    })
                    mainproducts = mainproducts.substr(1);
                    $('#mainClass').append("<input type='hidden' name='product2' value='" + mainproducts + "'>");
                    $('.purchase_create_butn').addClass('blue');
                    $.post("/purchase/create_back.jsp", $("#create_form").serializeArray(), function (data) {
                        if (data == 1) {
                            tanchuan("恭喜发布成功", "1", "/purchase/create", "1");
                        } else {
                            tanchuan("请勿重复发布", "0", "", "0");
                        }
                    })
                }
            } else {
                tanchuan("请确认项目信息是否填完整", "0", "", "0");
            }
        }
    }
});
/*source*/
/*截止时间*/
$(".ti").click(function () {
    if ($(this).attr("id") == "ti") {
        $(this).css({"background": "#016cdc", "color": "#fafafa"});
        $("#indate").val("");
        $(this).text("长期有效");
        $("#indate").attr("onFocus", "WdatePicker({lang: 'zh-cn', dateFmt: 'yyyy-MM-dd'})").attr("class", "Wdate");
        $(this).attr("id", "");
    } else {
        $(this).css({"background": "#f0f0f0", "color": "#353535"});
        $("#indate").val($(this).text());
        $(this).text("取消长期有效");
        $("#indate").attr("onFocus", "").attr("class", "");
        $(this).attr("id", "ti");
    }
})
/*source提交*/
$("#source_create_content input,textarea").blur(function () {
    if (login.complete("name") && $("#indate").val() != "" && $("#mainClass").children().length != 0)
    {
        $('.create_butn').addClass('blue');
    } else {
        $('.create_butn').removeClass('blue');
    }
})
$(".create_butn").click(function ()
{
    if (Cookie.Get("account") == "" || Cookie.Get("account") == null) {
        tanchuan("请登陆", "3", "/login/login", "1");
    } else {
        if (login.complete("name") && $("#indate").val() != "" && $("#mainClass").children().length != 0)
        {
            if ($("#indate").val() == "长期有效") {
                $("#indate").val("2057-10-01");
            }
            var nowtimes = new Date().getTime();
            var tim = $("#indate").val().split(" ")[0];
            tim = tim.replace(/-/g, '/');
            tim = new Date(tim).getTime();
            if (nowtimes > tim) {
                tanchuan("截止时间必须大于当前时间", "0", "", "0");
            } else {
                $('.create_butn').addClass('blue');
                var mainproducts = "";
                $('#mainClass input').each(function () {
                    mainproducts += "," + $(this).val();
                })
                mainproducts = mainproducts.substr(1);
                $('#mainClass').append("<input type='hidden' name='product2' value='" + mainproducts + "'>");
                $.post("/source/create_back.jsp", $("#create_form").serializeArray(), function (data) {
                    if (data == 1) {
                        tanchuan("恭喜发布成功", "1", "/source/create", "1");
                    } else if (data == 2) {
                        tanchuan("请勿重复发布", "0", "", "0");
                    } else {
                        tanchuan("发布失败", "0", "", "0");
                    }
                })
            }
        } else {
            tanchuan("请确认项目信息是否填完整", "0", "", "0");
        }
    }
});
/*添加物料*/
var tbody = $('#table tbody'), html = tbody.find("tr").eq(0), selectimgid = 0;
function add()
{
    var len = tbody.find('tr').length;
    var tr = html.clone();
    var ipt = tr.find("input");
    tr.find(".num").text(len + 1);
    ipt.eq(1).attr("name", "materielname[" + len + "]");
    ipt.eq(2).attr("name", "model[" + len + "]");
    ipt.eq(3).attr("name", "amout[" + len + "]");
    ipt.eq(4).attr("name", "unit[" + len + "]");
    ipt.eq(5).attr("name", "delivery_date[" + len + "]");
    ipt.eq(6).attr("name", "attachurl[" + len + "]");
    tbody.append(tr);
    tr.find("img").attr("imgid", len);
}
/*物料删除*/
$('.purchase_create_tablebot h2').click(function ()
{
    var ids = $("[class='purchase_create_checkbox']:checked");
    var vals = [];
    ids.each(function ()
    {
        vals.push(this.value);
    });
    var i;
    if (vals == "")
    {
        $('.purchase_create_butn').removeClass('blue');
        tanchuan("请选择要删除的行", "2", "", "0");
    } else
    {
        $(".purchase_create_checkbox:checked").parent().parent().remove();
        var long = $('#table tbody tr').length;
        for (var i = 0; i < long; i++)
        {
            $('#table tbody').find(".num").eq(i).text(i + 1);
            $('#table tbody').find(".purchase_create_checkbox").eq(i).val(i + 1);
            $('#table tbody tr').eq(i).find('[class="materielname"]').attr("name", "materielname[" + i + "]");
            $('#table tbody tr').eq(i).find('[class="model"]').attr("name", "model[" + i + "]");
            $('#table tbody tr').eq(i).find('[class="amout"]').attr("name", "amout[" + i + "]");
            $('#table tbody tr').eq(i).find('[class="unit"]').attr("name", "unit[" + i + "]");
            $('#table tbody tr').eq(i).find('[class="Wdate"]').attr("name", "delivery_date[" + i + "]");
            $('#table tbody tr').eq(i).find('.imgupload ul').attr('id', 'icourlwrap' + (i));
            $('#table tbody tr').eq(i).find('[class="imgshow"]').attr("swfupload", "icourlswf" + (i));
            $('#table tbody tr').eq(i).find('[class="imgurl"]').attr('id', 'ico' + (i));
            $('#table tbody tr').eq(i).find('[id="ico' + (i) + '"]').attr("name", "attachurl[" + (i) + "]");
        }
    }
    if ($('#table tbody').find("tr").text() == "")
    {
        add();
    }
});
/*图片显示*/
$("tbody").on("click", ".uploadimg", function () {
    selectimgid = $(this).attr("imgid");
    var position = $(this).parent().siblings().eq(1).text();
    $(".imgupload").css("margin-top", 851 + 43 * position + "px");
    $(".imgupload").toggle();
    if ($(this).attr("src") != "/purchase/img/img-1.png") {
        $(".swfuploadimg img").attr("src", $(this).attr("src"))
    }
});
$(".purchase_create_form1 span").click(function () {
    $(this).addClass("selectd").siblings().removeClass("selectd");
    var purchase_class = $(this).attr("value");
    $("#purchase_class").val(purchase_class);
})
/*主营产品*/
$("#product1").change(function () {
    $("#product2 option:gt(0)").remove();
    var val = $(this).val();
    if (!isNaN(val))
        new Ajax("/purchase/create_business.jsp").keyvalue("product1", val).getJson(function (data) {
            $.each(data, function (key, val) {
                $("#product2").append("<option>" + val['category'] + "</option>")
            })
        })
})
$('#product2').change(function () {
    var val = $(this).val();
    if (val != "请选择") {
        $('#mainClass').append('<span><input value="' + val + '" readOnly="true"/><em>Ｘ</em></span>');
    }
    $('#mainClass em').on('click', function () {
        $(this).parent().remove();
    });
});
/*提示语*/
$("#table").on("blur",$(".unit"),function () {
        var unit = $(this).val();
        if (!isNaN(unit) || unit == "") {
            $(this).val("");
            tanchuan("物料表中单位不能位数字或空", "0", "", "0");
        }
})

